package com.example.book.config.configImpl;

import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSON;
import com.example.book.util.AjaxResult;
import com.example.book.util.ServletUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * @author 绿松石
 * @version V1.0
 * @Title AuthenticationEntryPointImpl
 * @Description
 * @date 2022年05月13日 16:01
 * @since V1.0
 */
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, Serializable {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        int code = HttpStatus.HTTP_UNAUTHORIZED;
        String msg = String.format("请求访问：{}，认证失败，无法访问系统资源", request.getRequestURI());
        ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(code, msg)));
    }
}
